package com.tingyuge.thinking.spring.i18n;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;

/**
 * 自定义 spring boot 场景下的 {@link MessageSource}
 *
 * @author xiaodao
 * @version 1.0
 * @date 2021/8/29
 * @see MessageSourceAutoConfiguration
 * @see ReloadableResourceBundleMessageSource
 */
@EnableAutoConfiguration
public class CustomizedMessageSourceDemo {
    /***
     * 在spring boot 场景中 primary configuration Source 高于 *autoConfiguration
     * @return
     */
    @Bean
    public MessageSource messageSouce(){
        return new ReloadableResourceBundleMessageSource();
    }
    public static void main(String[] args) {
//        new SpringApplicationBuilder(CustomizedMessageSourceDemo.class).web(WebApplicationType.NONE).run(args)
        final ConfigurableApplicationContext applicationContext = SpringApplication.run(CustomizedMessageSourceDemo.class, args);
        final ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
        if(beanFactory.containsBean(AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME)){
            //查找 MessageSource的 BeanDefinition
            System.out.println(beanFactory.getBeanDefinition(AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME));
            //查找MessageSource的bean
            final MessageSource bean = applicationContext.getBean(AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME, MessageSource.class);
            System.out.println(bean);
        }
        applicationContext.close();
    }
}
